package com.api.gbhg.daizhishun.util;

import com.api.gbhg.daizhishun.Vo.BudgetExecuteVo;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: daizhishun
 * @author: daizhishun
 * @date: 2021/4/26
 **/
//生成预算执行通知单 规则：年月序号-->2021040001
public class GenerateBudgetJobOrder {
    public Map<String,String> GenerateBudgetJobOrder() {
            RecordSet rs = new RecordSet();
            Map<String,String> map = new HashMap<>();
            SimpleDateFormat nian = new SimpleDateFormat("yyyy");
            SimpleDateFormat yue = new SimpleDateFormat("MM");
            String year = nian.format(new Date());
            String month = yue.format(new Date());
            String str = "select nian,yue,Max(xh) AS xh from uf_budgetexecute GROUP BY  nian,yue ";
            rs.executeQuery(str);
            if (rs.next()){
                map.put("nian",rs.getString("nian"));
                map.put("yue",rs.getString("yue"));
                map.put("xh",rs.getString("xh"));
                return map;
            }else{
                String xx = "xx";
                map.put("nian",year);
                map.put("yue",month);
                map.put("xh",xx);
                return map;
            }
        }


        public String result(){
            SimpleDateFormat nian = new SimpleDateFormat("yyyy");
            SimpleDateFormat yue = new SimpleDateFormat("MM");
            String year = nian.format(new Date());
            String month = yue.format(new Date());
            String zw = "000";
            Map<String, String> stringmap = GenerateBudgetJobOrder();
            //如果map里,当前年月,序号为XX,则表示数据库无数据,获取当前年月，序号为0001
            if((stringmap.get("nian").equals(year) && stringmap.get("yue").equals(month)) && stringmap.get("xh").equals("xx")){
                RecordSet rs = new RecordSet();
                String str = "INSERT INTO uf_budgetexecute(nian,yue,xh) VALUES('"+year+"','"+month+"','"+zw+"1')";
                rs.executeUpdate(str);
            }else{

                //如果map里的数据来自数据库，判断当前年月,如果对应，则让序号加1，并保存到数据库
                if (stringmap.get("nian").equals(year) && stringmap.get("yue").equals(month)){
                    RecordSet rs = new RecordSet();
                    Integer xh = Integer.parseInt(stringmap.get("xh"));
                    Integer str = ++xh;
                    String str1 = "INSERT INTO uf_budgetexecute(nian,yue,xh) VALUES('"+year+"','"+month+"',rtrim(ltrim(right(cast('"+ zw +"'+rtrim(CAST("+ str +" as int)) as varchar(20)),4))))";
                    rs.executeUpdate(str1) ;
                }else {
                    //如果map里的数据不是当前年月,则执行初始化数据库，表示新的一个月或是新的一年
                    RecordSet rs = new RecordSet();
                    String str = "truncate table uf_budgetexecute";
                    rs.executeUpdate(str);
                    String str1 = "INSERT INTO uf_budgetexecute(nian,yue,xh) VALUES('"+year+"','"+month+"','"+zw+"1')";
                    rs.executeUpdate(str1);
                }
            }
                RecordSet rs1 = new RecordSet();
                StringBuffer stringBuffer = new StringBuffer();
                String str1 = "select nian,yue,Max(xh) AS xh from uf_budgetexecute GROUP BY  nian,yue";
                rs1.executeQuery(str1);
                while (rs1.next()){
                    stringBuffer.append(rs1.getString("nian"));
                    stringBuffer.append(rs1.getString("yue"));
                    stringBuffer.append(rs1.getString("xh"));
                }
                return stringBuffer.toString();
        }
}
